﻿Imports System.Drawing
Imports System.Threading.Tasks
Imports System.Threading

Public Class ERGBMapGenerator
    Private m_MapSize As New Size(160, 240)
    Private m_LockMap As New Object()

    Public Sub GenerateMap(callback As Action(Of Bitmap))
        ThreadPool.QueueUserWorkItem(
            Sub()
                Dim map = New Bitmap(m_MapSize.Width, m_MapSize.Height)
                FillRectangle(map, New Point(0, 0), map.Size, ERGBColors.Erase)

                For Each t_color In {ERGBColors.Cloud, ERGBColors.Smelter, ERGBColors.Seed}
                    For i = 0 To RealRandom.Rand(1, 5)
                        FillRectangle(
                            map,
                            New Point(RealRandom.Rand(0, map.Width - 1), RealRandom.Rand(0, map.Height - 1)),
                            New Point(RealRandom.Rand(0, map.Width - 1), RealRandom.Rand(0, map.Height - 1)),
                            t_color
                        )
                    Next
                Next

                Debug.Print("callback")
                callback(map)
            End Sub
        )
    End Sub

    Private Sub FillRectangle(map As Bitmap, start As Point, [end] As Point, color As Color)
        FillRectangle(map, start.X, start.Y, [end].X, [end].Y, color)
    End Sub

    Private Sub FillRectangle(map As Bitmap, start As Point, [end] As Size, color As Color)
        FillRectangle(map, start.X, start.Y, [end].Width - 1, [end].Height - 1, color)
    End Sub

    Private Sub FillRectangle(map As Bitmap, xStart As Integer, yStart As Integer, xEnd As Integer, yEnd As Integer, color As Color)
        For x = Math.Min(xStart, xEnd) To Math.Max(xStart, xEnd)
            For y = Math.Min(yStart, yEnd) To Math.Max(yStart, yEnd)
                map.SetPixel(x, y, color)
            Next
        Next
    End Sub
End Class
